<?php

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class Admin extends Model
{
    //软删除
    use SoftDelete;

    //只读字段
    protected $readonly = ['email'];

    //登录校验
    public function login($data)
    {
        $validate = new \app\common\validate\Admin();
        if (!$validate->scene('login')->check($data)){
            return $validate->getError();
        }
        $data['password'] = md5s($data['password']);
        $result = $this -> where($data) -> find();
        if ($result){
            //判断用户是否可用
            if ($result['status'] == 0){
                return '该用户已被禁用！';
            }
            $sessionData = [
                'id' => $result['id'],
                'nickname' =>  $result['nickname'],
                'is_super' => $result['is_super'],
                'email' => $result['email']
            ];
            session('admin',$sessionData);
            //用户名和密码正确
            return 1;
        } else {
            return '用户名或者密码错误！';
        }
    }

    //注册账户校验
    public function register($data)
    {
        $validate = new \app\common\validate\Admin();
        if (!$validate->scene('register')->check($data)){
            return $validate->getError();
        }
        $data['password'] = md5s($data['password']);
        $result = $this -> allowField(true) -> save($data);
        if ($result){
            mailto($data['email'],'管理员账户注册成功！','欢迎注册笔下光年后台管理员，您已成功注册管理员账户，请妥善保管密码！');
            return 1;
        } else {
            return '注册失败！';
        }
    }

    //重置密码校验--发送验证码
    public function forget($data)
    {
        $validate = new \app\common\validate\Admin();
        if (!$validate->scene('forget')->check($data)){
            return $validate->getError();
        }
        $result = $this -> where($data) -> find();
        if ($result){
            $num = random(6);
            $code = ['num'=>$num,'user'=>$data['username'],'settime'=>time()+300];
            session('code',$code);
            mailto($result['email'],'密码重置---随机验证码','您的随机验证码是：'.$num.'(5分钟内有效)');
            return 1;
        } else {
            return '用户名不存在！';
        }

    }

    //修改密码
    public function reset($data)
    {
        $validate = new \app\common\validate\Admin();
        if (!$validate->scene('reset')->check($data)){
            return $validate -> getError();
        }
        $se = session('code');
        if ($se['settime'] < time()){
            return '验证码已失效，请重新获取！';
        }
        if ($se['num'] != $data['code']){
            return '验证码输入错误！';
        }
        $data['password'] = md5s($data['password']);
        $adminInfo = $this ->where('username',$se['user'])->find();
        $adminInfo -> password = $data['password'];
        $result = $adminInfo -> save();
        if ($result){
            unset($_SESSION['code']);
            $content = '恭喜您，密码重置成功！<br>用户名：'.$adminInfo['username'].'<br>邮箱：'.$adminInfo['email'];
            mailto($adminInfo['email'],'密码重置成功！',$content);
            return 1;
        } else {
            return '密码重置失败！';
        }
    }




}
